package com.chunuo.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.chunuo.gulimall.product.entity.ProductAttrValueEntity;
import com.chunuo.gulimall.product.vo.AttrVo;
import com.chunuo.gulimall.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chunuo.gulimall.product.entity.AttrEntity;
import com.chunuo.gulimall.product.service.AttrService;
import com.chunuo.common.utils.PageUtils;
import com.chunuo.common.utils.R;
import sun.rmi.runtime.Log;


/**
 * 商品属性
 *
 * @author liujiamiao
 * @email 2544296058@gmail.com
 * @date 2020-06-25 18:42:45
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @GetMapping("/base/listforspu/{spuId}")
    public R baseListForspu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> list = productAttrValueService.queryListBySpuId(spuId);

        return R.ok().put("data",list);
    }

    /**
     * 更新规格
     * @param spuId
     * @param entitys
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateListForspu(@PathVariable("spuId")Long spuId,
                              @RequestBody List<ProductAttrValueEntity> entitys){
        productAttrValueService.updateListForspu(spuId,entitys);
        return R.ok();
    }


    @GetMapping("/{type}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("type")String type){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrVo respVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
//		attrService.save(attr);
        attrService.saveDetail(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
//		attrService.updateById(attr);
        attrService.updateDetail(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
